package administrator;

import java.awt.EventQueue;

public class Admin_Login {

	public JFrame frame;
	private JPasswordField passwordField;
	private JTextField UserIDField;
	public String user_name = " ";
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Admin_Login window = new Admin_Login();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	
	public Admin_Login() {
		initialize();
	}

	
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(400, 200, 500, 400);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBackground(new Color(255, 69, 0));
		panel.setBounds(0, 22, 484, 69);
		frame.getContentPane().add(panel);
		panel.setLayout(null);
		
		JLabel lblLogin = new JLabel("LOGIN DETAILS\r\n");
		lblLogin.setBounds(10, 11, 454, 44);
		panel.add(lblLogin);
		lblLogin.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblLogin.setHorizontalAlignment(SwingConstants.CENTER);
		
		JLabel lblUserId = new JLabel("USER ID");
		lblUserId.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblUserId.setHorizontalAlignment(SwingConstants.CENTER);
		lblUserId.setBounds(33, 121, 178, 44);
		frame.getContentPane().add(lblUserId);
		
		JLabel lblPassword = new JLabel("PASSWORD");
		lblPassword.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblPassword.setHorizontalAlignment(SwingConstants.CENTER);
		lblPassword.setBounds(33, 187, 178, 44);
		frame.getContentPane().add(lblPassword);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(253, 201, 165, 20);
		frame.getContentPane().add(passwordField);
		
		UserIDField = new JTextField();
		UserIDField.setBounds(253, 135, 165, 20);
		frame.getContentPane().add(UserIDField);
		UserIDField.setColumns(10);
		
		JButton btnLogin = new JButton("LOGIN");
		btnLogin.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				
				btnLoginActionPerformed(evt);
			}
		});
		btnLogin.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnLogin.setBounds(201, 303, 89, 23);
		frame.getContentPane().add(btnLogin);
	}
	
	public int getUser_Pass(String user_name, String pass)
	{
		String st1;
		int flag = 0;
		st1= "select * from login"; 
		try
        {
            Class.forName("java.sql.Driver");
            
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3307/oats","root","anupam24");
            
            Statement stmt = con.createStatement();
            
            ResultSet rs = stmt.executeQuery(st1);
           
            String user = "";
            String password = "";
            
            while (rs.next())
            {
            	
            	 user = rs.getString(1);
            	 password = rs.getString(2);
            	 
            	 if(user.equals(user_name) && password.equals(pass))
            	 {
            		
         			flag =1;		// for checking whether the username and password is correct or not, if its 0 then not found
         			         			
            	 }
            }
            
        }
		catch(Exception e)
        {
            System.out.println("Error is : "+e.getMessage());
        }
		
		return flag;
	}
	
	
	
	private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {
							
			String pass = "";
			
			user_name = UserIDField.getText();
			pass = new String(passwordField.getPassword());
			
			int flag;
			
			flag = getUser_Pass(user_name,pass);
			
			if (flag == 0)
	        {
	        	
	    			JOptionPane.showMessageDialog(null, " User ID / Password is Wrong\n","Login Fail", JOptionPane.ERROR_MESSAGE);
	    		
	        }
			
			if(flag == 1)
			{
			
			//System.out.println(user_name);
			//System.out.println(pass);
			
			Admn_Menu am = new Admn_Menu();
			am.frame.setVisible(true);
			this.frame.setVisible(false);
			am.Display(user_name);
			}
			
		}
}

